from common_import import *
import pandas as pd

# 预先读取所有CSV文件并缓存到内存中
df_crops = pd.read_csv("data/1_乡村种植的农作物.csv")  # 包含作物信息
df_land_info = pd.read_csv("data/1_乡村的现有耕地.csv")  # 包含地块信息
df_stats = pd.read_csv("data/2_2023年统计的相关数据.csv")  # 包含统计数据

# region index
lands_index = {
    "A1": 0,
    "A2": 1,
    "A3": 2,
    "A4": 3,
    "A5": 4,
    "A6": 5,
    "B1": 6,
    "B2": 7,
    "B3": 8,
    "B4": 9,
    "B5": 10,
    "B6": 11,
    "B7": 12,
    "B8": 13,
    "B9": 14,
    "B10": 15,
    "B11": 16,
    "B12": 17,
    "B13": 18,
    "B14": 19,
    "C1": 20,
    "C2": 21,
    "C3": 22,
    "C4": 23,
    "C5": 24,
    "C6": 25,
    "D1": 26,
    "D2": 27,
    "D3": 28,
    "D4": 29,
    "D5": 30,
    "D6": 31,
    "D7": 32,
    "D8": 33,
    "E1": 34,
    "E2": 35,
    "E3": 36,
    "E4": 37,
    "E5": 38,
    "E6": 39,
    "E7": 40,
    "E8": 41,
    "E9": 42,
    "E10": 43,
    "E11": 44,
    "E12": 45,
    "E13": 46,
    "E14": 47,
    "E15": 48,
    "E16": 49,
    "F1": 50,
    "F2": 51,
    "F3": 52,
    "F4": 53,
}

crops_index = {
    "黄豆": 0,
    "黑豆": 1,
    "红豆": 2,
    "绿豆": 3,
    "爬豆": 4,
    "小麦": 5,
    "玉米": 6,
    "谷子": 7,
    "高粱": 8,
    "黍子": 9,
    "荞麦": 10,
    "南瓜": 11,
    "红薯": 12,
    "莜麦": 13,
    "大麦": 14,
    "水稻": 15,
    "豇豆": 16,
    "刀豆": 17,
    "芸豆": 18,
    "土豆": 19,
    "西红柿": 20,
    "茄子": 21,
    "菠菜 ": 22,
    "青椒": 23,
    "菜花": 24,
    "包菜": 25,
    "油麦菜": 26,
    "小青菜": 27,
    "黄瓜": 28,
    "生菜 ": 29,
    "辣椒": 30,
    "空心菜": 31,
    "黄心菜": 32,
    "芹菜": 33,
    "大白菜": 34,
    "白萝卜": 35,
    "红萝卜": 36,
    "榆黄菇": 37,
    "香菇": 38,
    "白灵菇": 39,
    "羊肚菌": 40,
}

# endregion


def is_bean(crop_info):
    """
    判断输入的作物是否是豆类
    """
    # 转换作物编号为字符串以防止类型问题
    crop_info_str = str(crop_info)

    # 根据输入的编号或作物名称创建布尔掩码
    mask = (df_crops["作物编号"].astype(str) == crop_info_str) | (
        df_crops["作物名称"] == crop_info
    )

    # 过滤出匹配的行
    matched_rows = df_crops[mask]

    # 检查其作物类型是否包含 "豆类"
    is_bean_result = matched_rows["作物类型"].str.contains("豆类").any()
    return is_bean_result


df_land_info = pd.read_csv("data/1_乡村的现有耕地.csv")  # 包含地块信息


def get_land_type(land_name):
    """
    根据地块名称获取地块类型
    """
    # 获取对应地块的类型
    land_type = df_land_info.loc[
        df_land_info["地块名称"] == land_name, "地块类型"
    ].values

    if land_type.size > 0:
        return land_type[0]
    else:
        return None


def get_land_area(land_name):
    """
    根据地块名称获取地块面积
    """
    # 获取对应地块的面积
    land_area = df_land_info.loc[
        df_land_info["地块名称"] == land_name, "地块面积/亩"
    ].values

    if land_area.size > 0:
        return land_area[0]
    else:
        return None


def get_crop_name(crop_number):
    """
    根据作物编号获取作物名称
    """
    # 获取对应作物的名称
    crop_name = df_crops.loc[df_crops["作物编号"] == crop_number, "作物名称"].values

    if crop_name.size > 0:
        return crop_name[0]
    else:
        return None


def get_yield_per_acre(crop_number, land_type, season):
    """
    根据作物编号、地块类型、季度获取亩产量
    """
    # 根据作物编号、地块类型和种植季次筛选亩产量
    if land_type == "智慧大棚" and season == "第一季":
        land_type = "普通大棚 "
    yield_value = df_stats.loc[
        (df_stats["作物编号"] == crop_number)
        & (df_stats["地块类型"] == land_type)
        & (df_stats["种植季次"] == season),
        "亩产量/斤",
    ].values

    if yield_value.size > 0:
        return yield_value[0]
    else:
        print(f"获取产量失败 {crop_number} {land_type} {season}")
        return None


df = pd.read_csv("data/利润.csv")
# 创建字典，key为 (CropID, FieldType), value为 Profit_per_Mu
profit_dict = {
    (row["CropID"], row["FieldType"], row["PlantingSeason"]): row["Profit_per_Jin"]
    for _, row in df.iterrows()
}
yield_dict = {
    (row["CropID"], row["FieldType"], row["PlantingSeason"]): row["Yield_per_Mu"]
    for _, row in df.iterrows()
}
cost_dict = {
    (row["CropID"], row["FieldType"], row["PlantingSeason"]): row["Cost_per_Mu"]
    for _, row in df.iterrows()
}

price_dict = {
    (row["CropID"], row["FieldType"], row["PlantingSeason"]): row["AvgPrice"]
    for _, row in df.iterrows()
}


# 定义通过作物号和地块类型获取利润的函数
def get_profit_idtype(crop_id, field_type, season):
    # 获取字典中的值，如果不存在则返回"未找到利润"
    if field_type == "智慧大棚" and season == "第一季":
        field_type = "普通大棚 "
    profit = profit_dict.get((crop_id, field_type, season))

    if profit is None:
        print(f"未找到利润 {crop_id}")
        return "未找到利润"
    return profit


# 定义通过作物号和地块类型获取成本的函数
def get_cost_idtype(crop_id, field_type, season):
    # 获取字典中的值，如果不存在则返回"未找到利润"
    if field_type == "智慧大棚" and season == "第一季":
        field_type = "普通大棚 "
    cost = cost_dict.get((crop_id, field_type, season))

    if cost is None:
        print(f"未找到成本 {crop_id}")
        return "未找到成本"
    return cost


# 定义通过作物号和地块类型获取产量的函数
def get_yield_idtype(crop_id, field_type, season):
    if field_type == "智慧大棚" and season == "第一季":
        field_type = "普通大棚 "
    y = yield_dict.get((crop_id, field_type, season))
    if y is None:
        print("未找到产量")
        return "未找到产量"
    return y


# 定义通过作物号和地块类型获取价格的函数
def get_price_idtype(crop_id, field_type, season):
    if field_type == "智慧大棚" and season == "第一季":
        field_type = "普通大棚 "
    y = price_dict.get((crop_id, field_type, season))
    if y is None:
        print("未找到价格")
        return "未找到价格"
    return y


price_dict = {
    (row["CropID"], row["FieldType"], row["PlantingSeason"]): row["AvgPrice"]
    for _, row in df.iterrows()
}


def get_priceperjin_idtype(crop_id, season):
    # 获取字典中的值，如果不存在则返回"未找到售价"
    field_type = "梯田"
    price = price_dict.get((crop_id, field_type, season))
    if price is None:
        print("未找到售价")
        return "未找到售价"
    return price


def get_priceperjin_idtypeD(crop_id, season=None):
    # 获取地块类型
    field_type = "水浇地"

    # 如果季节为None，则依次尝试“单季”，“第一季”，“第二季”
    if season is None:
        possible_seasons = ["单季", "第一季", "第二季"]
        for s in possible_seasons:
            price = price_dict.get((crop_id, field_type, s))
            if price is not None:
                return price
        # 如果所有季节都未找到
        print(f"{crop_id} 未找到销售售价")
        return "未找到售价"
    else:
        # 如果提供了season，则直接查找
        price = price_dict.get((crop_id, field_type, season))
        if price is None:
            print(f"{crop_id} 未找到销售价")
            return "未找到售价"
        return price


def get_priceperjin_idtypeE(crop_id, season=None):
    # 获取地块类型
    field_type = "普通大棚 "

    # 如果季节为None，则依次尝试“单季”，“第一季”，“第二季”
    if season is None:
        possible_seasons = ["单季", "第一季", "第二季"]
        for s in possible_seasons:
            price = price_dict.get((crop_id, field_type, s))
            if price is not None:
                return price
        # 如果所有季节都未找到
        print(f"{crop_id} 未找到销售售价")
        return "未找到售价"
    else:
        # 如果提供了season，则直接查找
        price = price_dict.get((crop_id, field_type, season))
        if price is None:
            print(f"{crop_id} 未找到销售价")
            return "未找到售价"
        return price


def get_priceperjin_idtypeF(crop_id, season=None):
    # 获取地块类型
    field_type = "智慧大棚 "

    # 如果季节为None，则依次尝试“单季”，“第一季”，“第二季”
    if season is None:
        possible_seasons = ["第二季"]
        for s in possible_seasons:
            price = price_dict.get((crop_id, field_type, s))
            if price is not None:
                return price
            price = price_dict.get((crop_id, "普通大棚 ", "第一季"))
            if price is not None:
                return price
        # 如果所有季节都未找到
        print(f"{crop_id} 未找到销售售价")
        return "未找到售价"
    else:
        # 如果提供了season，则直接查找
        price = price_dict.get((crop_id, field_type, season))
        if price is None:
            print(f"{crop_id} 未找到销售价")
            return "未找到售价"
        return price


if __name__ == "__main__":
    pass
    # print(get_yield_per_acre(1, "智慧大棚", "单季"))
